package com.example.myone.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myone.Adapter.MyArticleAdapter;
import com.example.myone.Entity.Article;
import com.example.myone.Entity.ArticleCategoryEnum;
import com.example.myone.Mapper.ArticleMapper;
import com.example.myone.R;
import com.example.myone.Util.LoginInfoUtil;

import java.util.List;
import java.util.Objects;

public class UserArticleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_pager_article);
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        // 返回
        ImageView topBack = findViewById(R.id.top_back_button);
        topBack.setOnClickListener(v -> finish());
        // 标题
        TextView topTitle = findViewById(R.id.top_title);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        if (ArticleCategoryEnum.ALL.getMessage().equals(title)) {
            topTitle.setText("全部");
            // 获取当前用户的文章
            System.out.println(LoginInfoUtil.getUser(this));
            List<Article> articleList = ArticleMapper.getArticleByAuthorId(
                    this, Objects.requireNonNull(LoginInfoUtil.getUser(this)).id);
            MyArticleAdapter myArticleAdapter = new MyArticleAdapter(this, articleList);
            recyclerView.setAdapter(myArticleAdapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
        } else {
            topTitle.setText(title);
            // 获取当前用户的点赞对应分类的文章
            System.out.println(LoginInfoUtil.getUser(this));
            List<Article> articleList =
                    ArticleMapper.getLikedArticlesByCategory(
                            this, Objects.requireNonNull(LoginInfoUtil.getUser(this)).id, title);
            MyArticleAdapter myArticleAdapter = new MyArticleAdapter(this, articleList);
            recyclerView.setAdapter(myArticleAdapter);
            recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        }

    }
}
